package com.hospital.common.core.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 *  AES工具类
 *  <pre>
 *    因为某些国家的进口管制限制，Java发布的运行环境包中的加解密有一定的限制。比如默认不允许256位密钥的AES加解密，解决方法就是修改策略文件。
 *   替换的文件：%JDK_HOME%\jre\lib\security\local_policy.jar
 *   参考： http://czj4451.iteye.com/blog/1986483
 *
 * @author lvyao
 * @version 1.0
 * @date 2022/4/8
 */
public class AESUtils {
    // 密钥
    public static String key = "b30ba7987600ea3c13b5d4e1b5421139";
    private static String charset = "utf-8";
    /**
     * 设置偏移量
     * 本例采用ECB加密模式，不需要设置偏移量
     */
    private static int offset = 0;
    private static String transformation = "AES/ECB/PKCS5Padding";
    private static String algorithm = "AES";


    /**
     * 加密
     *
     * @param content
     * @return
     */
    public static String encrypt(String content) {
        return encrypt(content, key);
    }

    /**
     * 解密
     *
     * @param content
     * @return
     */
    public static String decrypt(String content) {
        return decrypt(content, key);
    }

    /**
     * 加密
     *
     * @param content
     *            需要加密的内容
     * @param key
     *            加密密钥
     * @return
     */
    public static String encrypt(String content, String key) {
        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            Cipher cipher = Cipher.getInstance(transformation);
            byte[] byteContent = content.getBytes(charset);
            cipher.init(Cipher.ENCRYPT_MODE, skey);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            BASE64Encoder base64 = new BASE64Encoder();
            return base64.encode(result); // 加密
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES（256）解密
     *
     * @param content
     *            待解密内容
     * @param key
     *            解密密钥
     * @return 解密之后
     * @throws Exception
     */
    public static String decrypt(String content, String key) {
        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE, skey);// 初始化
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] result = cipher.doFinal(base64Decoder.decodeBuffer(content));
            return new String(result,"UTF-8"); // 解密
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        String s = "{\n" +
                "\t\"businessno\": \"承保业务号\",\n" +
                "\t\"isCommonFlag\": null\n}";
        // 加密
        System.out.println("加密前：" + s);
        String encryptResultStr = encrypt(s);
        System.out.println("加密后：" + encryptResultStr);
        // 解密
        System.out.println("解密后：" + decrypt(encryptResultStr));
    }
}
